package com.example.servlet;

import cn.hutool.json.JSONUtil;
import com.example.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;

@WebServlet(name="demo02", value = "/demo02")
@MultipartConfig // 该注解支持servlet上传
public class ServletDemo02 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        // 通过参数名获取参数type
        String type = req.getParameter("type");
        if (type != null) {
            switch (type) {
                case "1":
                    doCase1(req, resp);
                    break;
                case "2":
                    doCase2(req, resp);
                    break;
                case "3":
                    doCase3(req, resp);
                    break;
                case "4":
                    doCase4(req, resp);
                    break;
                case "5":
                    doCase5(req, resp);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp)
            throws ServletException, IOException {
        // 后端转给doGet方法进行分发
        doGet(req, resp); // 不是在发get请求，仅仅只是代码调用
    }

    /* 获取路径参数 */
    private void doCase1(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException, IOException {
        String name = req.getParameter("name"); // 中文乱码？
        System.out.println(name);
        String age = req.getParameter("age");
        // 参数转类型
        int ageInt = Integer.parseInt(age);
        System.out.println(age);
        // 获取不存在的参数不会触发异常，结果是null
        String sex = req.getParameter("sex");
        System.out.println(sex);
    }

    /* 遍历所有参数 */
    private void doCase2(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException, IOException {
        String queryString = req.getQueryString();
        System.out.println(queryString); // ?后面的部分
        // 获取所有的参数名称
        Enumeration<String> parameterNames =
                req.getParameterNames();
        // 遍历参数名集合
        while (parameterNames.hasMoreElements()) {
            // 参数名
            String name = parameterNames.nextElement();
            // 参数值
            String value = req.getParameter(name);
            System.out.println(name + " : " + value);
        }
    }

    /* 表单GET方式提交 */
    private void doCase3(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
    }

    /* POST请求复杂数据 */
    private void doCase4(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        String msg = req.getParameter("msg"); // json字符串
        // JSON字符串转成Java对象
        // 要先定义跟JSON格式一样的类结构
        Book bean = JSONUtil.toBean(msg, Book.class);
        System.out.println(bean);
    }

    /* POST文件上传 */
    private void doCase5(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException, IOException {
        String filename = req.getParameter("filename");
        System.out.println(filename);
        //req.getInputStream() // 会读取所有的请求数据，包括请求行、请求头、请求体
        // 根据文件的参数名获取文件
        Part part = req.getPart("attachment");
        // 获取真实文件名
        String submittedFileName = part.getSubmittedFileName();
        System.out.println(submittedFileName);
        // 保存文件和读取内容只能做一个
        // 保存文件，需要保证文件夹已存在
        // part.write("F:/data/上传的文件/" + submittedFileName);
        // 纯文本才可以读内容
        InputStream is = part.getInputStream();
        // 转换成字符流
        InputStreamReader reader = new InputStreamReader(is);
        // 转成可以读一行内容的字符输入流
        BufferedReader br = new BufferedReader(reader);
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
        reader.close();
        is.close();
    }
}
